#include <stdio.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/ledc.h"
#include "esp_err.h"
//输出的GPIO口
#define LED_PIN_R    (2)    // LED连接的引脚
#define LED_PIN_G    (13)    // LED连接的引脚
#define LED_PIN_B    (15)    // LED连接的引脚
//LED使用的PWM通道
#define CHANNEL_1           LEDC_CHANNEL_0
#define CHANNEL_2           LEDC_CHANNEL_1
#define CHANNEL_3           LEDC_CHANNEL_2
// PWM的频率
#define LED_FREQ_HZ 5000
//PWM分辨率
#define LEDC_DUTY_RES           LEDC_TIMER_8_BIT
// 设置PWM为高速模式
#define LEDC_MODE               LEDC_HIGH_SPEED_MODE
// 设置PWM的定时器
#define LEDC_TIMER              LEDC_TIMER_0
// 设置LED通道 GPIO2
ledc_channel_config_t ledc_channel_1 = {
        .gpio_num   = LED_PIN_R,                             // 设置PWM输出引脚
        .speed_mode = LEDC_MODE,                // 设置PWM为高速模式
        .channel    = CHANNEL_1,                         // 设置PWM通道
        .intr_type  = LEDC_INTR_DISABLE,                   // 设置禁用中断
        .timer_sel  = LEDC_TIMER_0,                         // 设置使用的PWM定时器
        .duty       = 0,                                     // 初始占空比为0
        .hpoint     = 0,
    };
 //设置LED通道 GPIO13
    ledc_channel_config_t ledc_channel_2 = {
        .gpio_num   = LED_PIN_G,                             // 设置PWM输出引脚
        .speed_mode = LEDC_MODE,                // 设置PWM为高速模式
        .channel    = CHANNEL_2,                         // 设置PWM通道
        .intr_type  = LEDC_INTR_DISABLE,                   // 设置禁用中断
        .timer_sel  = LEDC_TIMER_0,                         // 设置使用的PWM定时器
        .duty       = 0 ,                                    // 初始占空比为0
        .hpoint     = 0,
    };
//设置LED通道 GPIO15
    ledc_channel_config_t ledc_channel_3 = {
        .gpio_num   = LED_PIN_B,                             // 设置PWM输出引脚
        .speed_mode = LEDC_MODE,                // 设置PWM为高速模式
        .channel    = CHANNEL_3,                         // 设置PWM通道
        .intr_type  = LEDC_INTR_DISABLE,                   // 设置禁用中断
        .timer_sel  = LEDC_TIMER_0,                         // 设置使用的PWM定时器
        .duty       = 0,                                     // 初始占空比为0
        .hpoint     = 0,
    };


#define DUTY_MAX 255
void led1_task(void *pvParameters)
{
    ledc_set_duty(ledc_channel_1.speed_mode, ledc_channel_1.channel, 0);
    ledc_update_duty(ledc_channel_1.speed_mode, ledc_channel_1.channel);

    while (1)
    {
        for (int duty = 0; duty <= DUTY_MAX; duty++)
        {
            ledc_set_duty(ledc_channel_1.speed_mode, ledc_channel_1.channel, duty);
            ledc_update_duty(ledc_channel_1.speed_mode, ledc_channel_1.channel);
            vTaskDelay(13 / portTICK_PERIOD_MS);
        }
        for (int duty = DUTY_MAX; duty >= 0; duty--)
        {
            ledc_set_duty(ledc_channel_1.speed_mode, ledc_channel_1.channel, duty);
            ledc_update_duty(ledc_channel_1.speed_mode, ledc_channel_1.channel);
            vTaskDelay(13 / portTICK_PERIOD_MS);

        }

    }
}
void led2_task(void *pvParameters)
{
    ledc_set_duty(ledc_channel_2.speed_mode, ledc_channel_2.channel, 0);
    ledc_update_duty(ledc_channel_2.speed_mode, ledc_channel_2.channel);
    vTaskDelay(666/portTICK_PERIOD_MS);
    while (1)
    {
        for (int duty = 0; duty <= DUTY_MAX; duty++)
        {
            ledc_set_duty(ledc_channel_2.speed_mode, ledc_channel_2.channel, duty);
            ledc_update_duty(ledc_channel_2.speed_mode, ledc_channel_2.channel);
            vTaskDelay(23 / portTICK_PERIOD_MS);
        }
        for (int duty = DUTY_MAX; duty >= 0; duty--)
        {
            ledc_set_duty(ledc_channel_2.speed_mode, ledc_channel_2.channel, duty);
            ledc_update_duty(ledc_channel_2.speed_mode, ledc_channel_2.channel);
            vTaskDelay(23 / portTICK_PERIOD_MS);

        }

    }
}
void led3_task(void *pvParameters)
{
    ledc_set_duty(ledc_channel_3.speed_mode, ledc_channel_3.channel, 0);
    ledc_update_duty(ledc_channel_3.speed_mode, ledc_channel_3.channel);


    vTaskDelay(666*2/portTICK_PERIOD_MS);
    while (1)
    {
        for (int duty = 66; duty <= DUTY_MAX; duty++)
        {
            ledc_set_duty(ledc_channel_3.speed_mode, ledc_channel_3.channel, duty);
            ledc_update_duty(ledc_channel_3.speed_mode, ledc_channel_3.channel);
            vTaskDelay(31 / portTICK_PERIOD_MS);
        }
        for (int duty = DUTY_MAX; duty >= 66; duty--)
        {
            ledc_set_duty(ledc_channel_3.speed_mode, ledc_channel_3.channel, duty);
            ledc_update_duty(ledc_channel_3.speed_mode, ledc_channel_3.channel);
            vTaskDelay(31 / portTICK_PERIOD_MS);

        }

    }
}


void ledc_init(void)
{
    ledc_timer_config_t ledc_timer = {
        .duty_resolution = LEDC_DUTY_RES,              // 设置PWM的分辨率为8位
        .freq_hz    = LED_FREQ_HZ,                        // 设置PWM的频率
        .speed_mode = LEDC_MODE,               // 设置PWM为高速模式
        .timer_num  = LEDC_TIMER ,                        // 设置PWM的定时器
        .clk_cfg    = LEDC_AUTO_CLK,                   //设置PWM信号的占空比分辨率（即占空比的位数）
    };
    ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_1));
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_2));
    ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel_3));

    xTaskCreate(led1_task, "LED Task 1", 2048, NULL, 13, NULL);
    xTaskCreate(led2_task, "LED Task 2", 2048, NULL, 13, NULL);
    xTaskCreate(led3_task, "LED Task 3", 2048, NULL, 13, NULL);
}
